package com.zwj.service.impl;

import com.zwj.dto.Result;
import com.zwj.entity.NodeDiscussEntity;
import com.zwj.mapper.NodeDiscussMapper;
import com.zwj.mapper.SysMenuMapper;
import com.zwj.mapper.SysUserMapper;
import com.zwj.service.NodeDiscussService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zwj.utils.DateUtils;
import com.zwj.utils.DiscussTree;
import com.zwj.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxj
 * @since 2021-11-02
 */
@Service
public class NodeDiscussServiceImpl extends ServiceImpl<NodeDiscussMapper, NodeDiscussEntity> implements NodeDiscussService {
    private final NodeDiscussMapper nodeDiscussMapper;
    private final SysUserMapper sysUserMapper;

    public NodeDiscussServiceImpl(NodeDiscussMapper nodeDiscussMapper,SysUserMapper sysUserMapper) {
        this.nodeDiscussMapper = nodeDiscussMapper;
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public Result selectNodeDiscuss(NodeDiscussEntity nodeDiscussEntity) {
        List<NodeDiscussEntity> result = nodeDiscussMapper.selectNodeDiscuss(nodeDiscussEntity);
        DiscussTree menuTree = new DiscussTree();
        List<Object> list = new ArrayList<Object>();
        list = menuTree.menuList(result);
        if (list == null||list.isEmpty()){
            return Result.ok().message("查询结果为空！");
        }
        return Result.ok().data(list);
    }

    @Override
    public Result addNodeDiscuss(NodeDiscussEntity nodeDiscussEntity, HttpServletRequest request) {
        String userID = TokenUtils.getUserID(request);
        String userName = sysUserMapper.selectNameById(userID);
        System.out.println(userName);
        nodeDiscussEntity.setIsDeleted(0);
        nodeDiscussEntity.setCreateBy(userName);
        nodeDiscussEntity.setCreateTime(DateUtils.getCurrentTime());
        int res = nodeDiscussMapper.insert(nodeDiscussEntity);
        if (res<1){
            return Result.error().message("评论失败！");
        }
        return Result.ok();
    }

    @Override
    public Result selectNodeDiscussList(NodeDiscussEntity nodeDiscussEntity) {
        List<NodeDiscussEntity> result = nodeDiscussMapper.selectNodeDiscussList(nodeDiscussEntity);

        if (result == null||result.isEmpty()){
            return Result.ok().message("查询结果为空！");
        }
        return Result.ok().data(result);
    }
}
